package marksys.free.com.marksys.ui.main;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.tencent.bugly.beta.Beta;

import marksys.free.com.marksys.databinding.ActivityMainBinding;
import marksys.free.com.marksys.R;
import marksys.free.com.marksys.nettool.Constant;
import marksys.free.com.marksys.ui.SettingActivity;
import marksys.free.com.marksys.ui.user.LoginActivity;
import marksys.free.com.marksys.ui.user.RecordActivity;
import marksys.free.com.marksys.utils.ActivityManager;
import marksys.free.com.marksys.utils.SharedPreferencesUtils;

public class MainActivity extends AppCompatActivity {
    public static final String NAME = "NAME";
    public static final String TYPE = "TYPE";
    public static final int key = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        String name = getIntent().getStringExtra(NAME);
        int type=getIntent().getIntExtra(TYPE,0);
        if(type==1){
            binding.nameText.setText(String.format("%s同学！", name));
        }else if(type==2){
            binding.nameText.setText(String.format("%s老师！", name));
        }
        binding.versionBtn.setOnClickListener(v -> {
            Beta.checkUpgrade();
        });
        binding.leaveBtn.setOnClickListener((View view) ->initDialog());
        binding.photoBtn.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, SettingActivity.class);
            intent.putExtra(SettingActivity.KEY,key);
            startActivity(intent);
        });
        binding.historyBtn.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, RecordActivity.class);
            startActivity(intent);
        });
    }

    public void initDialog() {
        AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("提示");
        builder.setMessage("确认退出当前帐号？");
        builder.setPositiveButton("确认退出", (dialog, which) -> {
            SharedPreferencesUtils.saveString( Constant.UserToken,"");
            ActivityManager instance = ActivityManager.getInstance();
            instance.clearAll();
            Intent intent = new Intent(MainActivity.this,LoginActivity.class);
            startActivity(intent);
            this.finish();
        });
        builder.setNegativeButton("取消",null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

}
